﻿using UnityEngine;
using System.Collections;

[System.Serializable]
public class RaycastCollider2D
{
    public enum TypeOfCollider
    {
        Overhead,
        Underfoot
    }

    [SerializeField]
    private Transform target;
    public Vector2 offset;

    public float length;
    public float width;

    public LayerMask checkLayer;

    private TypeOfCollider typeOfCollider;

    public Transform Target
    {
        get
        {
            return target;
        }

        set
        {
            target = value;
        }
    }

    public RaycastCollider2D()
    {
        switch (typeOfCollider)
        {
            case TypeOfCollider.Overhead:
                offset = new Vector2(0, 0.5f);
                break;
            case TypeOfCollider.Underfoot:
                offset = new Vector2(0, -0.5f);
                break;
        }
        length = 0.1f;
        width = 0.8f;
    }
    public RaycastCollider2D(TypeOfCollider type)
    {
        this.typeOfCollider = type;
        switch (typeOfCollider)
        {
            case TypeOfCollider.Overhead:
                offset = new Vector2(0, 0.5f);
                break;
            case TypeOfCollider.Underfoot:
                offset = new Vector2(0, -0.5f);
                break;
        }
        length = 0.1f;
        width = 0.8f;
    }

    public bool IsColliding()
    {
        if (Physics2D.Raycast((Vector2)Target.position + offset + new Vector2(-width / 2, 0), GetVectorForDirection(), length, checkLayer.value)) return true;
        if (Physics2D.Raycast((Vector2)Target.position + offset, GetVectorForDirection(), length, checkLayer.value)) return true;
        if (Physics2D.Raycast((Vector2)Target.position + offset + new Vector2(+width / 2, 0), GetVectorForDirection(), length, checkLayer.value)) return true;
        if (Physics2D.Raycast((Vector2)Target.position + offset + new Vector2(-width / 2, length * GetVectorForDirection().y), Vector2.right, width, checkLayer.value)) return true;
        return false;
    }


    public void DrawRayCast()
    {
        if (Target == null)
            return;
        switch (typeOfCollider)
        {
            case TypeOfCollider.Overhead:
                Gizmos.color = Color.red;
                break;
            case TypeOfCollider.Underfoot:
                Gizmos.color = Color.green;
                break;
        }
        Gizmos.DrawLine((Vector2)Target.position + offset + new Vector2(-width / 2, 0), (Vector2)Target.position + offset + new Vector2(width / 2, 0));
        Gizmos.DrawLine((Vector2)Target.position + offset + new Vector2(-width / 2, length * GetVectorForDirection().y), (Vector2)Target.position + offset + new Vector2(width / 2, length * GetVectorForDirection().y));
        Gizmos.DrawLine((Vector2)Target.position + offset + new Vector2(-width / 2, 0), (Vector2)Target.position + offset + new Vector2(-width / 2, length * GetVectorForDirection().y));
        Gizmos.DrawLine((Vector2)Target.position + offset + new Vector2(width / 2, 0), (Vector2)Target.position + offset + new Vector2(width / 2, length * GetVectorForDirection().y));
    }

    public Vector2 GetVectorForDirection()
    {
        switch (typeOfCollider)
        {
            case TypeOfCollider.Overhead: return Vector2.up;
            case TypeOfCollider.Underfoot: return Vector2.down;
        }
        return Vector2.zero;
    }

}